<template>
  <div class="school-management">
    <h2>学校管理</h2>
    <div class="action-buttons">
      <el-button type="primary" @click="showAddDialog">添加学校</el-button>
    </div>
    <el-table :data="schools" style="width: 100%">
      <el-table-column prop="id" label="学校ID" width="180" />
      <el-table-column prop="name" label="学校名称" />
      <el-table-column prop="short_name" label="简称" />
      <el-table-column prop="region" label="地区" />
      <el-table-column prop="school_type" label="学校类型" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="editSchool(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteSchool(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '添加学校' : '编辑学校'"
      width="30%"
    >
      <el-form :model="form" label-width="80px">
        <el-form-item label="学校名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="简称">
          <el-input v-model="form.short_name" />
        </el-form-item>
        <el-form-item label="地区">
          <el-input v-model="form.region" />
        </el-form-item>
        <el-form-item label="学校类型">
          <el-select v-model="form.school_type" placeholder="请选择学校类型">
            <el-option label="小学" value="小学" />
            <el-option label="中学" value="中学" />
          </el-select>
        </el-form-item>
        <el-form-item label="Logo">
          <el-input v-model="form.logo_url" placeholder="Logo URL" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

const schools = ref([])
const dialogVisible = ref(false)
const dialogType = ref('add')
const form = ref({
  id: null,
  name: '',
  short_name: '',
  region: '',
  school_type: '',
  logo_url: ''
})

// 获取学校列表
const fetchSchools = async () => {
  try {
    const response = await axios.get('http://localhost:3000/api/schools')
    schools.value = response.data
  } catch (error) {
    ElMessage.error('获取学校列表失败')
  }
}

// 显示添加对话框
const showAddDialog = () => {
  dialogType.value = 'add'
  form.value = {
    name: '',
    short_name: '',
    region: '',
    school_type: '',
    logo_url: ''
  }
  dialogVisible.value = true
}

// 编辑学校
const editSchool = (school) => {
  dialogType.value = 'edit'
  form.value = { ...school }
  dialogVisible.value = true
}

// 删除学校
const deleteSchool = async (id) => {
  try {
    await ElMessageBox.confirm('确定要删除这个学校吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await axios.delete(`http://localhost:3000/api/schools/${id}`)
    ElMessage.success('删除成功')
    fetchSchools()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

// 提交表单
const handleSubmit = async () => {
  try {
    if (dialogType.value === 'add') {
      await axios.post('http://localhost:3000/api/schools', form.value)
      ElMessage.success('添加成功')
    } else {
      await axios.put(`http://localhost:3000/api/schools/${form.value.id}`, form.value)
      ElMessage.success('更新成功')
    }
    dialogVisible.value = false
    fetchSchools()
  } catch (error) {
    ElMessage.error(dialogType.value === 'add' ? '添加失败' : '更新失败')
  }
}

onMounted(() => {
  fetchSchools()
})
</script>

<style scoped>
.school-management {
  padding: 20px;
}

.action-buttons {
  margin-bottom: 20px;
}
</style>